package cn.gxa.j229.bug.finance.controller.content;//package cn.gxa.j229.bug.finance.controller.content;

import cn.gxa.j229.bug.finance.pojo.Essay;
import cn.gxa.j229.bug.finance.pojo.TbIdea;
import cn.gxa.j229.bug.finance.service.contant.TbIdeaService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Jun
 * @version 1.0
 * @date 2020/9/21 15:01
 */

@Controller
@RequestMapping("/idea")
@CrossOrigin
public class IdeaController {

    @Resource
    TbIdeaService tbIdeaService;

    //添加
    @PostMapping("/add")
    @ResponseBody
    public Map add(@RequestBody TbIdea tbIdea){
        tbIdeaService.insert(tbIdea);
        System.out.println(tbIdea);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        jsonResult.setData(tbIdea);
        return jsonResult.getValues();
    }

    //根据id查询
    @PostMapping("/findById")
    @ResponseBody
    public Map findById(@RequestBody Essay essay){
        List<Map<String,Object>> list =tbIdeaService.selectById(essay.getId());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询id为" +essay.getId()+"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }

    //根据状态查询
    @PostMapping("/findByState")
    @ResponseBody
    public Map selectByState(@RequestBody Essay essay){
        List<Map<String,Object>> list =tbIdeaService.selectByState(essay.getState());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询状态为" +essay.getState()+"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }

    //根据建议关键字查询
    @PostMapping("/findByIdea")
    @ResponseBody
    public Map findByPosition(@RequestBody TbIdea tbIdea){
        List<Map<String,Object>> list =tbIdeaService.selectByIdea(tbIdea.getIdea());
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询意见关键字为" + tbIdea.getIdea() +"的成功");
        jsonResult.setData(list);
        return jsonResult.getValues();

    }

    //查询总记录数
    @PostMapping("/findCount")
    @ResponseBody
    public Map findCount(){
        List<Map<String,Object>> count =tbIdeaService.selectCount();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData("总记录数为:" + count);
        jsonResult.setMsg("成功");
        return jsonResult.getValues();

    }
    //分页查询
    @PostMapping("/findByPage")
    @ResponseBody
    public Map findByPage(@RequestBody TbIdea tbIdea){
        List<Map<String,Object>> list =tbIdeaService.selectTbIdeaByPage(tbIdea);
        int counts = tbIdeaService.selectByPageCount(tbIdea);
        System.out.println(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count",counts);
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询");
        jsonResult.setData(list);
        return jsonResult.getValues();
    }
    //修改
    @PostMapping("/update")
    @ResponseBody
    public Map update(@RequestBody TbIdea tbIdea){

        int update = tbIdeaService.update(tbIdea);
        System.out.println(tbIdea);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改了" + update + "行");
        jsonResult.setData(tbIdea);
        return jsonResult.getValues();
    }
    //删除
    @PostMapping("/delete")
    @ResponseBody
    public Map delete(@RequestBody TbIdea tbIdea){
        tbIdeaService.delete(tbIdea.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        jsonResult.setData("");
        return jsonResult.getValues();
    }
}
